import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index'
import api from '@/axios/api/index' // 引入封装好的axiosAPI接口
import { Toast } from 'vant'

Vue.use(Router)

const router = new Router({
  routes: [
    { //主页
      path: '/',
      name: 'index',
      component: () => import('@/views/index'),
    },

    { // 登录页
      path: '/login',
      name: 'login',
      component: () => import('@/views/login'),
    },
  ]
})

// 路由权限判定
let token = window.localStorage.getItem('token')  // token验证权限
router.beforeEach((to,from,next) => {
  // 先验证是不是去登录页和注册页
  if(to.fullPath == '/login'){
    store.commit('changeTopath','/')
    next()
  } else { // 不是的话就验证权限存储下一个要去的页面path
    if(to.meta.requireAuth) {
      if(!token) {
        store.commit('changeTopath',to.fullPath)
        next( 
          {
            path: '/login'
          }
        )
      } else {
        next()
      }
    } else {
      next()
    }
  }
  
})
        
export default router